package com.example.demo1.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo1.exception.ExceptionEnum;
import com.example.demo1.exception.ToprsException;
import com.example.demo1.model.User;
import com.example.demo1.response.Response;
import com.example.demo1.response.SuccessResponse;
import com.example.demo1.service.UserService;
import com.example.demo1.util.JWTUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "${basePath}/user")
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    UserService uService;

    @ApiImplicitParam(name = "id", defaultValue = "1", required = true, value = "用户ID")
    @GetMapping("/{id}")
    public Response getUserById(@PathVariable Integer id) {
        User u = uService.getUserByid(id);
        if (u != null)
            return new SuccessResponse(u);
        else
            return new Response(404, "该用户不存在", null);
    }

    @GetMapping("/getUserInfo")
    public Response getUserInfo(@RequestParam(value = "id", defaultValue = "-1") Integer id) {
        if (id <= 0)
            return Response.error(ExceptionEnum.NEED_USER_ID);
        User u = uService.getUserByid(id);
        if (u != null)
            return Response.success(u);
        else
            return new Response(404, "该用户不存在", null);
    }

    @PostMapping("/register")
    @ResponseBody
    public Response register(@RequestParam("userName") String userName, @RequestParam("password") String password)
            throws ToprsException {

        if (StringUtils.isEmpty(userName)) {
            return Response.error(ExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return Response.error(ExceptionEnum.NEED_PASSWORD);
        }

        uService.register(userName, password);

        return Response.success(null);
    }

    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(nickname = "years", notes = "用户登录", response = Response.class, value = "用户登录")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userName", required = true, value = "用户名"),
            @ApiImplicitParam(name = "password", required = true, value = "密码") })

    public Response login(@RequestParam("userName") String userName, @RequestParam("password") String password)
            throws Exception {

        if (StringUtils.isEmpty(userName)) {
            return Response.error(ExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return Response.error(ExceptionEnum.NEED_PASSWORD);
        }

        User user = uService.login(userName, password);

        // JSONObject map = JSONObject.parseObject(JSON.toJSONString(user));

        JSONObject json = (JSONObject) JSONObject.toJSON(user);
        json.put("token", JWTUtil.createToken(user.getId()));
        return Response.success(json);

    }

}
